import { Column, Entity, Index, PrimaryGeneratedColumn } from 'typeorm'

// 商品主表
@Entity('ug_goods')
export class GoodsEntity {
  @PrimaryGeneratedColumn({ name: 'goods_id', type: 'int' })
  goodsId: number

  @Index('idx_goods_supplier')
  @Column({ name: 'supplier_id', type: 'int' })
  supplierId: number

  @Index('idx_goods_category')
  @Column({ name: 'category_id', type: 'int' })
  categoryId: number

  @Column({ name: 'title', type: 'varchar', length: 120 })
  title: string

  @Column({ name: 'subtitle', type: 'varchar', length: 255, nullable: true })
  subtitle?: string

  @Column({ name: 'cover', type: 'varchar', length: 255, nullable: true })
  cover?: string

  @Column({ name: 'desc_html', type: 'mediumtext', nullable: true })
  descHtml?: string | null

  @Index('idx_goods_status')
  @Column({ name: 'status', type: 'tinyint', default: 1 })
  status: number

  @Column({ name: 'audit_status', type: 'tinyint', default: 1 })
  auditStatus: number

  @Column({ name: 'sales', type: 'int', default: 0 })
  sales: number
}

// 商品 SKU 表（简化版用于数量和价格管理）
@Entity('ug_sku')
export class SkuEntity {
  @PrimaryGeneratedColumn({ name: 'sku_id', type: 'int' })
  skuId: number

  @Index('idx_sku_goods')
  @Column({ name: 'goods_id', type: 'int' })
  goodsId: number

  @Column({ name: 'spec_json', type: 'json', nullable: true })
  specJson?: any

  @Column({ name: 'price', type: 'decimal', precision: 10, scale: 2 })
  price: string

  @Column({ name: 'origin_price', type: 'decimal', precision: 10, scale: 2, nullable: true })
  originPrice?: string

  @Column({ name: 'stock', type: 'int', default: 0 })
  stock: number

  @Column({ name: 'barcode', type: 'varchar', length: 64, nullable: true })
  barcode?: string

  @Column({ name: 'weight', type: 'decimal', precision: 10, scale: 2, nullable: true })
  weight?: string

  @Column({ name: 'status', type: 'tinyint', default: 1 })
  status: number
}

// 库存流水表
@Entity('ug_stock_log')
export class StockLogEntity {
  @PrimaryGeneratedColumn({ name: 'id', type: 'bigint' })
  id: number

  @Index('idx_stock_sku')
  @Column({ name: 'sku_id', type: 'int' })
  skuId: number

  @Column({ name: 'change', type: 'int' })
  change: number

  @Column({ name: 'biz_type', type: 'varchar', length: 30 })
  bizType: string

  @Column({ name: 'biz_no', type: 'varchar', length: 64, nullable: true })
  bizNo?: string

  @Column({ name: 'remark', type: 'varchar', length: 255, nullable: true })
  remark?: string

  @Column({ name: 'create_time', type: 'datetime', precision: 6, default: () => 'CURRENT_TIMESTAMP(6)' })
  createTime: Date
}
